<template>
  <div id="home">
    <van-nav-bar
      title="约德尔人图书馆"
      left-text="返回"
      right-text="按钮"
      left-arrow
      :fixed="true"
    />
    <van-pull-refresh
      v-model="isLoading"
      @refresh="onRefresh"
      :disabled="finished"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        ><ArticlesInfo
          v-for="item in list"
          :key="item.art_id"
          :title="item.title"
          :author="item.aut_name"
          :cmt-count="item.comm_count"
          :time="item.pubdate"
          :cover="item.cover"
        ></ArticlesInfo> </van-list
    ></van-pull-refresh>
  </div>
</template>

<script>
import { getArticleListAPI } from '@/api/articlesApi.js'
import ArticlesInfo from '@/components/Articles/ArticlesInfo.vue'
export default {
  name: 'Home',
  data() {
    return {
      list: [],
      pages: 1,
      limit: 10,
      loading: true,
      finished: false,
      isLoading: true,
    }
  },
  created() {
    this.getArticaleList()
  },
  methods: {
    async getArticaleList(ifRefresh) {
      const { data: res } = await getArticleListAPI(this.pages, this.limit)
      if (ifRefresh) {
        this.list = [...res, ...this.list]
        this.isLoading = false
      } else {
        // this.loading = true
        this.list = [...this.list, ...res]
        this.loading = false
      }
      if (res.length == 0) {
        this.finished = true
      }
    },
    onLoad() {
      this.pages++
      this.getArticaleList()
    },
    onRefresh() {
      this.pages++
      this.getArticaleList(true)
    },
  },
  components: {
    ArticlesInfo,
  },
}
</script>

<style lang="less" scoped>
#home {
  padding: 46px 0 50px 0;
  .van-nav-bar {
    background-color: #007bff;
    /deep/.van-nav-bar__title,
    /deep/.van-nav-bar__text,
    /deep/.van-nav-bar__arrow {
      color: white;
    }
  }
}
</style>
